<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增库存')" />
    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
    <!-- 图标库 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-warehousestock-add">
            <!-- 仓库编号 -->
            <input type="hidden" id="wId" name="wId" class="form-control" required readonly/>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">仓库名称：</label>
                <div class="col-sm-8">
                    <select id="warehouseName" name="warehouseName" class="form-control" required></select>
                </div>
            </div>
            <!-- 水果种类编号 -->
            <input type="hidden" id="fruittypesId" name="fruittypesId" class="form-control" required readonly>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">水果种类名称：</label>
                <div class="col-sm-8">
                    <select id="fruittypesName" name="fruittypesName" class="form-control" required>
                        <option disabled selected>请先选择仓库编号</option>
                    </select>
                </div>
            </div>
            <!-- 水果编号 -->
            <input type="hidden" id="fruitId" name="fruitId" class="form-control" required readonly>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">水果名称：</label>
                <div class="col-sm-8">
                    <select id="fruitName" name="fruitName" class="form-control" required>
                        <option disabled selected>请先选择水果种类</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="jinjia" class="col-sm-3 control-label is-required">进价(元/500g)：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="jinjia" name="jinjia" class="form-control" placeholder="请设置进价" type="text" oninput="this.value = this.value.replace(/[^0-9.]/g, '')" required>
                        <span class="input-group-addon"><i class="bi bi-currency-yen"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="whCount" class="col-sm-3 control-label is-required">重量(kg)：</label>
                <div class="col-sm-8">
                    <input id="whCount" name="whCount" placeholder="请输入进货量" class="form-control" type="text" required>
                </div>
            </div>
        </form>
    </div>
    <script th:inline="javascript">
        $(document).ready(function() {
            selectWarehouse(); // 查询仓库信息
        });

        // 查询所有的仓库信息
        var wareHouseObj = {}; // 创建一个空对象
        function selectWarehouse() {
            $.ajax({
                url: "/inventory/warehousestock/selectWid",
                dataType: "json",
                type: "post",
                success:function (resp) {
                    wareHouseObj = {}; // 清空对象
                    if (resp.warehouse.length > 1) {
                        $("<option disabled selected></option>").html("请选择仓库").appendTo("#warehouseName");
                        for (let i = 0; i < resp.warehouse.length; i++) {
                            var warehouse = resp.warehouse[i];
                            console.log("仓库名称" + (i+1) + ":" + warehouse.wName);
                            wareHouseObj[warehouse.wName] = warehouse.wNumber; // warehouse.wName作为键，warehouse.wNumber作为值存入对象中
                            $("<option></option>").html(warehouse.wName).appendTo("#warehouseName"); // 将所有仓库的编号添加到仓库名称下拉框中
                        }
                        console.log("所有的仓库:");
                        console.log(wareHouseObj);
                    } else {
                        for (let i = 0; i < resp.warehouse.length; i++) {
                            var warehouse = resp.warehouse[i];
                            console.log("仓库编号:" + warehouse.wNumber);
                            console.log("仓库名称:" + warehouse.wName);
                            $("<option></option>").html(warehouse.wName).appendTo("#warehouseName"); // 将所有仓库的编号添加到仓库名称下拉框中
                            $("#wId").val(warehouse.wNumber); // 将仓库编号插入仓库编号输入框中
                            selectFruitId(warehouse.wNumber, warehouse.wName); // 根据仓库编号,查询仓库中所有的水果编号
                            selectFruitType(); // 查询水果种类
                        }
                    }

                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }

        //监听仓库名称是否选中
        $(document).ready(function () {
            $("#warehouseName").on('change', function () {
                $("#fruitName").empty();// 清空水果下拉框的所有选项
                $("<option disabled selected></option>").html("请先选择水果种类").appendTo("#fruitName");
                $("#fruittypesId").val(""); // 每更改一次仓库就清空一次水果种类编号
                $("#fruitId").val(""); // 每更改一次仓库就清空一次水果编号
                let warehouseName = $("#warehouseName").val(); // 获取选择的仓库名称
                if (warehouseName !== null) {
                    console.log("选中的仓库:" + warehouseName)
                    let warehouseNumber = wareHouseObj[warehouseName]; // 根据选则的仓库名称找出对应的仓库编号
                    $("#wId").val(warehouseNumber); // 将仓库编号插入仓库编号输入框中
                    selectFruitId(warehouseNumber, warehouseName); // 根据仓库编号,查询仓库中所有的水果编号
                    selectFruitType(); // 查询水果种类
                }
            })
        })

        // 根据仓库编号查询水果编号
        let wIdOfFruitIdMap = []; // 创建一个空集合
        function selectFruitId(warehouseNumber, warehouseName) {
            $.ajax({
                url: "/inventory/warehousestock/selectFruitId",
                dataType: "json",
                data: {
                    "wId": warehouseNumber
                },
                type: "post",
                success: function (resp) {
                    wIdOfFruitIdMap = []; // 清空集合
                    for (let i = 0; i < resp.wIdOfFruitIdList.length; i++) {
                        wIdOfFruitIdMap[i] = resp.wIdOfFruitIdList[i]; // 将所有的水果编号插入集合中
                    }
                    console.log(warehouseName + "仓库中已有的水果集合:");
                    console.log(wIdOfFruitIdMap);
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }


        // 查询水果种类编号和水果种类名称 (状态正常的)
        var fruittypesObj = {}; // 创建一个空对象
        function selectFruitType() {
            $.ajax({
                url: "/inventory/fruittypes/name",
                dataType: "json",
                type: "post",
                success: function (resp) {
                    $("#fruittypesName").empty();
                    fruittypesObj = {}; // 清空对象
                    $("<option disabled selected></option>").html("请选择水果种类").appendTo("#fruittypesName");
                    for (let i = 0; i < resp.fruittypes.length; i++) {
                        var fruittypes = resp.fruittypes[i];
                        fruittypesObj[fruittypes.name] = fruittypes.code; // 将fruittypes.name作为键，fruittypes.code作为值存入对象中
                        $("<option></option>").html(fruittypes.name).appendTo("#fruittypesName");
                    }
                    console.log("所有的水果种类:");
                    console.log(fruittypesObj);
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }

        //监听水果种类名称,插入对应的水果种类编号
        $(document).ready(function () {
            $("#fruittypesName").on('change', function () {
                $("#fruitId").val(""); // 每更改一次水果种类就清空一次水果编号
                var fruittypesName = $("#fruittypesName").val(); // 获取选择的水果种类
                console.log("选中的水果种类:" + fruittypesName);
                var fruittypesCode = fruittypesObj[fruittypesName]; // 根据选中的水果种类查询对应的水果种类编号
                $("#fruittypesId").val(fruittypesCode); // 将选中的水果种类的编号插入输入框中
                selectFruit(fruittypesCode, fruittypesName); // 根据水果种类编号查询水果编号和水果名称 (只查询状态正常的水果)
            })
        })

        // 根据水果种类编号查询水果编号和水果名称 (只查询状态正常的水果)
        var fruitObj = {}; // 创建一个空对象
        function selectFruit(fruittypesCode, fruittypesName) {
            $.ajax({
                url: "/inventory/fruits/selectFruit",
                dataType: "json",
                data: {
                    "fruittypesId": fruittypesCode,
                    "isStatus" : 1
                },
                type: "post",
                success: function (resp) {
                    $("#fruitName").empty();
                    fruitObj = {}; // 清空对象
                    $("<option disabled selected></option>").html("请选择").appendTo("#fruitName");
                    for (let i = 0; i < resp.fruits.length; i++) {
                        var fruits = resp.fruits[i];
                        if (!wIdOfFruitIdMap.includes(fruits.code)) {
                            fruitObj[fruits.name] = fruits.code; // fruits.name作为键，fruits.code作为值存入对象中
                            $("<option></option>").html(fruits.name).appendTo("#fruitName"); //插入水果名称下拉框选项
                        }
                    }
                    console.log($("#warehouseName").val() + "中" + fruittypesName + "种类没有的水果:");
                    console.log(fruitObj);
                },
                error:function(xhr,type,errorThrown){
                    console.log('XMLHttpRequest 对象:',xhr);
                    console.log('错误类型:', type);
                    console.log('错误描述:', errorThrown);
                    $.modal.alertError("请求失败");
                }
            })
        }
        // 监听水果名称下拉框,如果选择,则插入对应的水果编号
        $(document).ready(function () {
            $("#fruitName").on('change', function () {
                var fruitName = $("#fruitName").val(); // 获取选中的水果名称
                console.log("选中的水果名称:" + fruitName);
                var fruitsCode = fruitObj[fruitName]; // 根据选中的水果名称 获取对应的水果编号
                $("#fruitId").val(fruitsCode); // 将选中的水果的编号插入输入框中
            })
        })
    </script>

    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "inventory/warehousestock"
        $("#form-warehousestock-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-warehousestock-add').serialize());
            }
        }
    </script>
</body>
</html>